package com.msti.service;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.msti.entity.Batch;
import com.msti.entity.Student;
import com.msti.sessionMgt.ApplicationSession;

public class StudentDAOImpl implements StudentDAO {

	@Override
	public String doRegister(Student student) {
		Session session = ApplicationSession.getSession();
		try {
			session.beginTransaction();
			//List<Batch> batches = student.getBatch();
			//for(Batch b : batches)
			//{
				//session.update(b);
				//List<Student> students = b.getStudents();
				/*for(Student s : students)
				{
					session.update(s);
				}*/
			//}
			session.update(student.getEmployee());
			session.save(student);
			session.getTransaction().commit();
		} catch (HibernateException he) {
			session.getTransaction().rollback();
			System.out.println(he);
		}
		finally{
			session.close();
		}
		return "SUCCESS";
	}

	@Override
	public List<Student> getAllStudent() {
		Session session = ApplicationSession.getSession();
		List<Student> allStudent = new ArrayList<Student>();
		try {
			Query query = session.createQuery("from Student");
			allStudent = query.list();
		} catch (HibernateException he) {
			session.getTransaction().rollback();
			System.out.println(he);
		}
		finally{
			session.close();
		}
		return allStudent;
	}

	@Override
	public Student getStudentById(String studentId) {

		return null;
	}

	@Override
	public List<Student> getAllUnConfirmedStudent() {
		Session session = ApplicationSession.getSession();
		List<Student> allUnConfirmedStudent = new ArrayList<Student>();
		try {
			long eId = 0;
			Query query = session.createQuery("from Student where employeeId = ?")
					.setParameter(0, eId);
			allUnConfirmedStudent = (List<Student>) query.list();
		} catch (HibernateException he) {
			session.getTransaction().rollback();
			System.out.println(he);
		}
		finally{
			session.close();
		}
		return allUnConfirmedStudent;
	}

	@Override
	public Student getStudentByCDC(String CDC) {
		Session session = ApplicationSession.getSession();
		Student student = new Student();
		try {
			Query query = session.createQuery("from Student where CDCNO = ?")
					.setParameter(0, CDC);
			student = (Student) query.uniqueResult();
		} catch (HibernateException he) {
			session.getTransaction().rollback();
			System.out.println(he);
		}
		finally{
			session.close();
		}
		return student;
	}

	@Override
	public Student getStudentByNIC(String NIC) {
		Session session = ApplicationSession.getSession();
		Student student = new Student();
		try {
			Query query = session.createQuery("from Student where NICNO = ?")
					.setParameter(0, NIC);
			student = (Student) query.uniqueResult();
		} catch (HibernateException he) {
			session.getTransaction().rollback();
			System.out.println(he);
		}
		finally{
			session.close();
		}
		return student;
	}

	@Override
	public Student getStudentByPassport(String Passport) {
		Session session = ApplicationSession.getSession();
		Student student = new Student();
		try {
			Query query = session.createQuery(
					"from Student where PassportNo = ?").setParameter(0,
					Passport);
			student = (Student) query.uniqueResult();
		} catch (HibernateException he) {
			session.getTransaction().rollback();
			System.out.println(he);
		}
		finally{
			session.close();
		}
		return student;
	}

	@Override
	public void doUpdate(Student student) {
		Session session = ApplicationSession.getSession();
        try {
            session.beginTransaction();
            session.update(student);
            session.getTransaction().commit();
        } catch (HibernateException e) {
        	session.getTransaction().rollback();
        }
		finally{
			session.close();
		}
		
	}

}
